
#include <stdio.h>


/** label for Life expectancy */
double gY[] = 
{
52.8,
76.8,
75.5,
84.6,
56.7,
72.3,
81.6,
80.4,
69.2,
72.2,
68.3,
75.3,
70.0,
79.6,
70.7,
59.7,
70.7,
71.2,
77.5,
53.2,
73.2,
73.2,
58.0,
59.1,
66.1,
56.6,
80.8,
70.4,
54.3,
78.5,
73.4,
76.2,
67.1,
57.5,
58.8,
79.8,
55.4,
76.2,
77.6,
80.0,
78.9,
61.8,
74.7,
70.2,
73.7,
60.1,
74.2,
60.0,
64.9,
79.6,
81.1,
75.11,
61.7,
65.7,
71.8,
80.0,
62.0,
80.2,
70.3,
70.8,
71.2,
57.1,
53.6,
65.0,
61.0,
71.8,
73.9,
82.4,
64.7,
69.4,
73.1,
66.6,
80.1,
80.6,
75.1,
82.5,
76.9,
67.1,
60.8,
77.3,
72.4,
44.5,
59.9,
75.6,
72.1,
81.0,
74.5,
62.2,
52.4,
74.5,
78.5,
58.5,
80.7,
65.3,
67.9,
72.9,
75.4,
70.4,
64.8,
76.0,
73.3,
54.0,
59.4,
59.1,
68.4,
80.3,
77.0,
58.0,
59.2,
80.8,
76.2,
64.1,
77.3,
58.6,
74.0,
78.2,
69.8,
75.4,
79.4,
77.9,
73.2,
67.9,
64.1,
72.3,
66.0,
63.5,
74.3,
53.6,
80.6,
74.9,
78.7,
52.6,
53.4,
81.1,
74.0,
65.5,
70.2,
45.1,
81.1,
82.0,
76.1,
69.6,
60.4,
73.9,
69.9,
57.5,
70.3,
71.7,
76.8,
77.8,
67.2,
56.0,
67.8,
75.6,
79.7,
78.3,
76.0,
69.6,
63.4,
74.1,
74.1,
51.1,
47.3,
};

/** the data of BMI */
double gX[] =
{
    20.62058 ,
    26.44657,
    24.5962 ,
    27.63048,
    22.25083,
    25.355420000000002,
    27.56373 ,
    26.467409999999997,
    25.65117,
    27.24594,
    20.39742,
    26.384390000000003,
    26.16443,
    26.75915,
    27.02255,
    22.41835,
    22.8218,
    24.43335,
    26.611629999999998,
    22.129839999999998,
    25.78623,
    26.542859999999997,
    21.27157,
    21.50291,
    20.80496,
    23.681729999999998,
    27.4521,
    23.515220000000003,
    21.485689999999998,
    27.015420000000002,
    22.92176,
    24.94041,
    22.06131,
    19.86692,
    21.87134,
    26.47897,
    22.56469,
    26.596290000000003,
    25.06867,
    27.41899,
    26.13287,
    23.38403,
    25.58841,
    26.732429999999997,
    26.36751,
    20.885089999999998,
    26.264459999999996,
    20.247,
    26.53078,
    26.733390000000004,
    25.853289999999998,
    30.867520000000003,
    24.0762,
    21.65029,
    25.54942,
    27.165090000000003,
    22.842470000000002,
    26.33786,
    26.01359,
    25.179879999999997,
    25.29947,
    22.52449,
    21.64338,
    23.68465,
    23.66302,
    25.10872,
    27.115679999999998,
    27.206870000000002,
    20.95956,
    21.85576,
    25.310029999999998,
    26.71017,
    27.65325,
    27.13151,
    24.00421,
    23.50004,
    27.47362,
    26.290779999999998,
    21.592579999999998,
    29.172109999999996,
    26.45693,
    21.90157,
    21.89537,
    26.54164,
    26.86102,
    27.434040000000003,
    26.34473,
    21.403470000000002,
    22.034679999999998,
    24.73069,
    23.219910000000002,
    21.78881,
    27.683609999999998,
    29.37337,
    22.62295,
    25.15669,
    27.42468,
    24.2369,
    24.88385,
    26.55412,
    25.63182,
    21.93536,
    21.44932,
    22.65008,
    20.76344,
    26.01541,
    25.77291,
    21.21958,
    23.03322,
    26.934240000000003,
    26.241090000000003,
    22.299139999999998,
    26.26959,
    25.015060000000002,
    25.54223,
    24.770410000000002,
    22.872629999999997,
    26.6738,
    26.68445,
    28.13138,
    25.41069,
    26.01131,
    22.55453,
    30.42475,
    23.51233,
    21.927429999999998,
    26.51495,
    22.53139,
    23.83996,
    26.92717,
    27.43983,
    21.969170000000002,
    26.85538,
    27.49975,
    21.96671,
    22.40484,
    25.49887,
    23.16969,
    26.37629,
    26.20195,
    26.919690000000003,
    23.77966,
    22.47792,
    23.008029999999998,
    20.59082,
    21.87875,
    30.99563,
    26.396690000000003,
    25.15699,
    26.703709999999997,
    25.24796,
    22.35833,
    25.42379,
    28.053590000000003,
    27.392490000000002,
    28.456979999999998,
    26.39123,
    25.32054,
    26.78926,
    26.5775,
    20.9163,
    20.68321,
    22.0266
};


int gEpochs = 1000;
double gLearning_rate = 0.001;

double gW = 1.0;
double gB = 0.0;

void linear_regression(double x[], int lenx, double y[])
{

    for(int i = 0; i < gEpochs; i++)
    {
        for (int j = 0; j < lenx; j++)
        {
            double y_hat = gW * x[j] + gB; 
            double delta = gLearning_rate * (y[j] - y_hat);

            gW += gW * delta;
            gB += delta;
        }
    }
}

double predict(double w, double b, double bmi)
{
    return w * bmi + b;
}
    



int main(int argc, char* argv[])
{

    linear_regression(gX, sizeof(gX) / sizeof(gX[0]), gY);
    double preBMI = 21.07931;
    double output = predict(gW, gB, preBMI);

    printf("parameters after train, w: %lf, b: %lf\n", gW, gB);

    printf("BMI: %lf, output life expectancy: %lf\n", preBMI, output);


    getchar();

    return 0;
}